import functools
class Solution(object):
    def minSessions(self, tasks, sessionTime):
        n = len(tasks)
        @functools.cache
        def cal(val):
            if val == 0:
                return 0
            else:
                index = 0
                while index < n:
                    if (val >> index) & 1:
                        break
                    else:
                        index += 1
                now_count = tasks[index]
                now_val = val ^ (1 << index)
                d = {now_val: now_count}
                index += 1
                while index < n:
                    next_d = d.copy()
                    if (val >> index) & 1:
                        for key in d:
                            if d[key] + tasks[index] <= sessionTime:
                                next_key = key ^ (1 << index)
                                next_d[next_key] = d[key] + tasks[index]
                    d = next_d
                    index += 1
                min_steps = float('inf')
                for key in d:
                   min_steps = min(min_steps, 1 + cal(key))
                return min_steps

        return cal(2 ** n - 1)

data = Solution()
tasks = [3,1,3,1,1]
se = 8
print(data.minSessions(tasks, se))